/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package EventHandler;

import app.bus.ProductModel;
import app.entities.Product;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mvc.Event;

/**
 *
 * @author Tuan Long
 */
public class AllProducts implements Event {

    @Override
    public String EventHandler(HttpServletRequest request, HttpServletResponse response) {

        int page = 1;
        int records = 12;
        if (request.getParameter("page") != null) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        int start_row = (page * records) - (records - 1);
        int end_row = start_row + (records - 1);

        List list = ProductModel.getListProduct(start_row, end_row);        
        int total_rows = ProductModel.getTotalProductRows();
        int no_page = ( (int)Math.ceil(total_rows / records));       
        request.setAttribute("list", list);
        request.setAttribute("start_row", start_row);
        request.setAttribute("end_row", end_row);
        request.setAttribute("no_page", no_page+1);
        request.setAttribute("currentPage", page);
        return "AllProducts.jsp";
        
    }

    public String test(HttpServletRequest request, HttpServletResponse response) {
        int page = 1;
        int records = 3;
        if (request.getParameter("page") != null) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        int start_row = (page * records) - (records - 1);
        int end_row = start_row + (records - 1);

        List list = ProductModel.getListProduct(start_row, end_row);

        int total_rows = ProductModel.getTotalProductRows();
        int no_page = (int) Math.ceil(total_rows * 1.0 / records);

        request.setAttribute("listProducts", list);
        request.setAttribute("start_row", start_row);
        request.setAttribute("end_row", end_row);
        request.setAttribute("no_page", no_page);
        request.setAttribute("currentPage", page);
        return "AllProducts.jsp";
    }
}
